package sedev.shopping.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sedev.shopping.entities.LoaiNhom;
import sedev.shopping.entities.ListLoaiNhom;
import sedev.shopping.util.Connector;
import sedev.shopping.util.HibernateUtil;
import sedev.shopping.util.SedevUtil;

public class LoaiNhomDAO {
	public LoaiNhomDAO(){
		
	}
	
	private LoaiNhom initData(ResultSet result_set){
		LoaiNhom data = new LoaiNhom();
		try{
			data.setId(result_set.getLong(LoaiNhom.Properties.id.name()));
        }catch(SQLException sqle){
        	data.setId(Long.valueOf(0));
        }
        try{
        	data.setNhomSanPhamId(result_set.getLong(LoaiNhom.Properties.nhom_san_pham_id.name()));
        }catch(SQLException sqle){
        	data.setNhomSanPhamId(Long.valueOf(0));
        } 
        try{
        	data.setLoaiSanPhamId(result_set.getLong(LoaiNhom.Properties.loai_san_pham_id.name()));
        }catch(SQLException sqle){
        	data.setLoaiSanPhamId(Long.valueOf(0));
        } 
		return data;
	}

	public boolean addData(LoaiNhom data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.save(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean updateData(LoaiNhom data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.update(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean removeData(Long id){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;		
		try{
			transaction = session.beginTransaction();			
			LoaiNhom data = new LoaiNhom();
			data.setId(id);
			session.delete(data);
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}		
		return true;
	}
	
	public LoaiNhom getData(Long id){
		LoaiNhom data = new LoaiNhom();		
		Session session = HibernateUtil.getSession().openSession();		
		try{
			session.load(data,id);		
		}catch(HibernateException ex){
			data = null;
		}
		return data;
	}
	
	public ListLoaiNhom getListData(String field, String condition, String order
									, Integer curr, Integer page_size, Long total_record){
		ListLoaiNhom listdata = new ListLoaiNhom();			
		Connector conn = Connector.getInstance();
		
		total_record = conn.getTotalRecord(LoaiNhom.Table, condition);
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append(field);
		sql.append(" from ").append(LoaiNhom.Table);
		sql.append(" where ").append(condition);
		if(order!=null && !order.equals("")){
			sql.append(" order by ").append(order);
		}
		if(page_size > 0){
            SedevUtil.checkPage(curr, page_size, total_record);
            Long offset = curr.longValue() - 1;
            offset = offset*page_size;
            sql.append(" limit ").append(offset).append(",").append(page_size);
        }
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null){
                while(rs.next()){                   
                   listdata.add(this.initData(rs));
                }
            }
        }catch(SQLException sqle){
        	listdata.clear();
        }
		return listdata;
	}
}
